import java.util.Stack;

class Mylinklist
{
    static class Linklist
    {
        public int val;
        public Linklist next;
        public Linklist pre;

        public Linklist(int val) {
            this.val = val;
        }
    }
    public Linklist head;
    public Linklist tail;
    public void dayin(Linklist head)
    {
        if(head==null)
        {
            return;
        }
        if(head.next==null)
        {
            System.out.println(head.val);
            return;
        }
        dayin(head.next);
        System.out.println(head.val);

    }
    public void clear()
    {
        Linklist a=this.head;
        while(a!=null)
        {
            Linklist c=a.next;
            a.next=null;
            a.pre=null;
            a=c;
        }
        head=null;
        tail=null;
    }
    public void removeall(int key)
    {

        Linklist b=this.head;
        while(b!=null)
        {

            if(key==head.val) {

                head = head.next;
                head.pre = null;
            }


            if(key==tail.val)
            {
                tail=tail.pre;
                tail.next=null;
            }



            if(b.val==key)
            {
                b.pre.next=b.next;
                b.next.pre=b.pre;

            }
            b=b.next;
        }




    }
    public void remove(int key)
    {
        if(key==head.val)
        {
            head=head.next;
            head.pre=null;

        }
        if(key==tail.val)
        {
            tail=tail.pre;
            tail.next=null;
        }
        Linklist b=this.head;
        while(b!=null)
        {
            if(b.val==key)
            {
                b.pre.next=b.next;
                b.next.pre=b.pre;
                return;
            }
            b=b.next;
        }

    }
    public void add(int i,int data)
    {
        Linklist a=new Linklist(data);
        if(i<=0||i>size())
        {
            System.out.println(i+"不合法");

        }
        if(i==1)
        {
            addtou(data);

        }
        if(i==size())
        {
            addwei(data);
        }
        Linklist v=find(i);
        a.next=v;
        Linklist f=v.pre;
        v.pre=a;
        f.next=a;
        a.pre=f;

    }
    public Linklist find(int i)
    {
        if(i<=0||i>size())
        {
            return null;
        }
        Linklist z=this.head;
         while(i-1!=0)
         {
            z=z.next;
            i--;

         }
         return z;
    }
    public void addwei(int data)
    {
        Linklist n=new Linklist(data);
        if(this.head==null)
        {
            this.head=n;
            this.tail=n;

        }
        else
        {
            this.tail.next=n;
            n.pre=this.tail;
            n.next=null;
            this.tail=n;

        }
    }
    public void addtou(int data)
    {
        Linklist m=new Linklist(data);
        if(this.head==null)
        {
            this.head=m;
            this.tail=m;
        }
        else
        {
            m.next=this.head;
            this.head.pre=m;
            this.head=m;
        }

    }
    public void creat()
    {
        Linklist a=new Linklist(10);
        Linklist b=new Linklist(20);
        Linklist c=new Linklist(30);
        this.head=a;
        this.tail=c;
        a.next=b;
        a.pre=null;
        b.next=c;
        c.next=null;
        c.pre=b;
        b.pre=a;



    }
    public void display()
    {
        Linklist e=this.head;
        while(e!=null)
        {

            System.out.print(e.val+" ");
            e=e.next;
        }
        System.out.println();


    }

    public int size()
    {
        int count=0;


        Linklist h=this.head;
        while(h!=null)
        {
            count++;
            h=h.next;

        }
        return count;
    }
    public boolean contains(int key)
    {
        Linklist c=this.head;
        while(c!=null)
        {
            if(c.val==key)
            {
                return true;
            }
            c=c.next;
        }
        return false;
    }








}









public class text13 {
    public static void main(String[] args) {
        Mylinklist a=new Mylinklist();
        //a.display();
        a.addtou(10);
        a.addtou(20);
        a.addtou(30);
        a.addtou(40);
        //a.clear();
        a.display();
        //a.dayin(a.head);
        Stack<Mylinklist. Linklist> b=new Stack<>();
        int i=0;
        Mylinklist. Linklist f=a.head;
        while(f!=null)
        {
            b.push(f);
            f=f.next;

        }

        while(!b.empty())
        {
            int x=b.pop().val;
            System.out.println(x);
        }




        //System.out.println(a.contains(40));
        /*a.addwei(50);
        a.addwei(60);
        a.addwei(50);
        a.addwei(50);

        a.display();
        System.out.println(a.size());
        Mylinklist.Linklist b=a.find(2);
        //System.out.println(b.val);
        a.add(2,70);
        a.display();
        //System.out.println();
        //a.remove(80);

        a.display();
        a.removeall(50);
        a.display();

        a.display();

         */






    }







}
